package pdfsetter.core.util;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * This is implementation of iterator for arbitrary {@link RandomAccessIterable}. It uses get and size methods in obvious way and stores current position -
 * index of next element to get.
 * 
 * @author Andrzej Dorobisz
 */
public class RandomAccessIterableIterator<T> implements Iterator<T> {
    private int position;
    private final RandomAccessIterable<T> randomAccessIterable;

    public RandomAccessIterableIterator(RandomAccessIterable<T> raIterable) {
        randomAccessIterable = raIterable;
        position = 0;
    }

    @Override
    public boolean hasNext() {
        return position < randomAccessIterable.size();
    }

    @Override
    public T next() {
        if (!hasNext())
            throw new NoSuchElementException();
        return randomAccessIterable.get(position++);
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }
}
